#!/usr/bin/env python
# -*- conding:utf-8 -*-
# 命令行执行：python .\analyseCollect.py --excel-path C:\Users\vainuer\Desktop\Test\test
import argparse
from datetime import datetime
from Tips import tips, configuration
from gevent.pool import Pool

from urllib import request
from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.sql import func
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

from openpyxl import Workbook
from openpyxl.styles import PatternFill
if __name__ == "__main__":
    parser = argparse.ArgumentParser()  # 创建对象
    parser.add_argument('--excel-path', required=True,
                        help='path of excel')  # 添加参数 True表示必要参数
    args = parser.parse_args()  # 解析参数

    # print(args)
    excel_path = args.excel_path
    # print(excel_path)
    aList = [64, 34, 25, 12, 22, 11, 90]
    # 引用其他模块中的函数
    tips.sum(aList)
    tips.bubbleSort(aList)

    print(tips.add(13, 17))
    print(tips.add(102.88, 203.12))

    aTuple = (1, 2, 10, 16, 22, 11, 25, 6, 7)
    tips.sum(aTuple)  # tuple 类型一旦初始化就不能修改，所以要是修改它就会报错。
    tips.bubbleSort(list(aTuple))  # 元组排序可以通过list函数将元组转换为列表，再采用列表排序方法

    pool = Pool()  # 创建进程池
    # 将数组中的每个元素提取出来作为函数的参数，创建一个个进程，放进进程中， 第一个参数是函数，第二个参数是一个迭代器，将迭代器中的数字作为函数的参数
    pool.map(tips.power, [i for i in range(2, 5)])
    # 作用：这是多进程的创建，应用在爬虫中的目的是提高爬取的效率，实现秒爬

    # 引用其他模块中的数据
    for i in configuration.analyzers:
        print(i)
    print(f"configuration.numberList拥有{len(configuration.numberList)} 条数据")
    print(f"路径：{configuration.database_path}")

    # 字符串连接方法，join()方法使用
    newString = ''.join(configuration.analyzers)
    print(newString)
    newString1 = '-'.join(['a', 'b', 'c', 'd', 'e'])
    print(newString1)

    # HTTP请求
    response = request.urlopen('https://www.baidu.com')
    # print(response.read().decode())  # 打印网页源码的文本信息
    print(response.status)  # 状态码
    # print(response.getheaders())  # 响应头

    # sqlalchemy模块
    # 创建表
    Base = declarative_base()  # 创建对象基类

    class Test(Base):
        __tablename__ = 'my_test'
        Id = Column(Integer, primary_key=True, autoincrement=True)
        Name = Column(String(128), nullable=False)
        NameId = Column(String(128), unique=True, nullable=False)
        CreateDatetime = Column(DateTime, default=func.now(), nullable=False)
        CreateDatetime1 = Column(
            DateTime, default=datetime.now(), nullable=False)

    database_engine = create_engine(
        configuration.DB_URL+'?charset=utf8mb4')  # 初始化数据库连接
    Base.metadata.create_all(database_engine)  # metadata.create_all创建所有表
    Session = sessionmaker(bind=database_engine)  # 创建Session
    database_session = Session()  # 实例化对象

    # 添加表数据
    test = Test(Name='Daniel', NameId='1001')
    database_session.add(test)
    database_session.commit()
    # 查询数据
    item_list = database_session.query(Test).all()
    for item in item_list:
        print(item.Id, item.Name, item.NameId,
              item.CreateDatetime, item.CreateDatetime1)
        database_session.close()
    Base.metadata.drop_all(database_engine)  # all table are deleted

    # openpyxl模块中Workbook
    # 创建一个工作簿对象
    wb = Workbook()
    ws = wb.active
    print('当前工作表：', ws)
    # 更改默认名称Sheet
    ws.title = 'NewSheet'
    # 单元格赋值
    ws['A1'] = 'Daniel'
    ws.cell(1, 2).value = '1001'
    c = ws['A1']
    d = ws.cell(1, 2)
    print(c.value, d.value)
    # 单元格填充颜色
    fill = PatternFill(fill_type='solid', fgColor='FFA0B1')
    d.fill = fill
    # 添加一行数据
    # ws.append(['Alice', '1001', 120, datetime.now()])  # 未设置样式，显示出问题
    ws.append(['Alice', '1002', 12, datetime.now().strftime('%Y-%m-%d')])
    ws.append(['Eason', '1003', 1001,
              datetime.now().strftime('%Y-%m-%d %H:%M:%S')])
    # 设置超链接
    ws.cell(5, 1, value='baidu').hyperlink = 'https://www.baidu.com'
    ws.cell(5, 1).style = 'Hyperlink'
    # 遍历单元格
    for row in ws.values:
        print(row)
    # 创建一个工作表
    ws = wb.create_sheet('Test')
    ws.sheet_properties.tabColor = 'FF72BA'
    wb.save(r'testFile\test.xlsx')
    wb.close()
